﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>菜单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiAdmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiAdmin/style/admin.css" media="all">
    <link rel="stylesheet" href="../../layuiAdmin//style/font-awesome/css/font-awesome.min.css">
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <label>标题：</label>
                    <div class="layui-inline">
                        <input type="text" name="title" placeholder="请输入标题名称" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-inline" id="parentId" style="width:180px ;">
                    </div>
                    <div class="layui-inline">
                        <select name="isParent">
                            <option value="">是否父级</option>
                            <option value="0">是</option>
                            <option value="1">否</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-useradmin" layadmin-event="reset">
                            <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="table-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body">
                <table id="module-manage" lay-filter="module-manage"></table>
            </div>
        </div>
    </div>
    <script src="../../layuiAdmin/layui/layui.js"></script>
    <script src="../../layuiAdmin/lib/extend/xm-select.js"></script>
    <script>
        layui.config({
            base: '../../layuiAdmin/', //静态资源所在路径
            version: top.layui.setter.version
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form', 'util'], function () {
            var $ = layui.$,
                form = layui.form,
                util = layui.util,
                table = layui.treeTable,
                admin = layui.admin;

            window.areaName = "module-manage";

            xmSelect.render({
                el: '#parentId',
                name: 'parentId',
                tips: '请选择父级菜单',
                model: { label: { type: 'text' } },
                radio: true,
                clickClose: true,
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [],
                },
                height: 'auto',
                data() {
                    var data = null;
                    admin.req({
                        type: 'get',
                        async: false,
                        url: '/api/Module/GetParentIdModule',
                        success: function (r) {
                            if (r.code == 200) {
                                data = r.data;
                            }
                        }
                    });
                    return data;
                }
            })

            //监听搜索
            form.on('submit(table-search)', function (data) {
                var field = data.field;
                //执行重载
                table.reload(areaName, {
                    where: { queryJson: JSON.stringify(field) },
                    page: {
                        curr: 1
                    }
                });
            });
            //用户管理
            table.render({
                elem: '#' + areaName,
                toolbar: $.getToolButtons(),
                defaultToolbar: [],
                url: '/api/Module/GetModulePageList',
                cols: [
                    [
                        /*  { type: 'checkbox', fixed: 'left' },*/
                        { field: 'number', title: '序号', width: 100, align: 'center', type: 'numbers', fixed: 'left' }, {
                            field: 'icon', title: '图标', width: 80, align: 'center', fixed: 'left', templet: function (d) {
                                if (d.icon.indexOf("fa") == 0) {
                                    return ' <i class="fa ' + d.icon + ' fa-2x" aria-hidden="true"></i>';
                                } else {
                                    return '<i class="layui-icon ' + d.icon + '" style="font-size:20px"></i>';
                                }
                            }
                        },
                        { field: 'name', title: '菜单名称', minWidth: 100, fixed: 'left' },
                        { field: 'path', title: '菜单路径', minWidth: 100 },
                        { field: 'sortCode', title: '排序', width: 100 },
                        { field: 'description', title: '描述', minWidth: 100 },
                        {
                            field: 'IsMenu',
                            minWidth: 80,
                            title: '是否父级菜单',
                            align: 'center',
                            templet: function (d) {
                                if (d.isMenu) return '是'
                                else return '否'
                            }
                        }, {
                            field: 'enabled',
                            width: 100,
                            title: '是否启用',
                            align: 'center',
                            templet: function (d) {
                                if (d.enabled) return '<button class="layui-btn layui-btn-xs">已启用</button>'
                                else return '<button class="layui-btn layui-btn-primary layui-btn-xs">已禁用</button>'
                            }
                        },
                        { title: '操作', width: 200, align: 'center', fixed: 'right', templet: () => $.getRowButtons() }
                    ]
                ],
                tree: { // treeTable 特定属性集
                    customName: { isParent: 'isMenu' },
                    view: { showIcon: false }
                },
                page: true,
                limit: 100,
                height: 'full-110',
                text: '对不起，加载出现异常！'
            });
            var selectRow = null;
            table.on('row(' + areaName + ')', function (obj) {
                selectRow = obj.data;
                obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            });

            var edit = function (id, title) {
                $.OpenTab({
                    type: 2,
                    title: '编辑菜单',
                    content: 'moduleform.html?id=' + id + "&title=" + title,
                    area: ['980px', '610px'],
                    btn: ['提交', '关闭'],
                    btn2: function (index, layero) {
                        layer.close(index);
                    }
                });
            }

            var del = function (type) {
                let ids = [];
                if (type == 1 || type == undefined) {
                    if (selectRow == null) return layer.msg('请选择数据');
                    ids = [selectRow.id];
                } else if (type == 2) {
                    var checkData = table.checkStatus(areaName).data; //得到选中的数据
                    if (checkData.length === 0) return layer.msg('请选择数据');
                    ids = checkData.map(t => t.id);
                } else {
                    return layer.msg('请选择删除类型');
                }

                layer.confirm('确定删除吗？', function (index) {
                    admin.req({
                        type: 'post',
                        url: '/api/module/DelModule',
                        data: { ids: ids.join(','), },
                        success: function (data) {
                            if (data.code == 200) {
                                table.reload(areaName);
                                layer.close(index);
                                layer.msg('已删除!', { icon: 1, time: 1000 });
                            } else {
                                layer.close(index);
                                layer.msg(data.msg, { icon: 2, time: 1000 });
                            }
                        }
                    });
                });
            }

            //事件
            util.event('lay-active', {
                'x-add': () => edit(),
                'x-delete': () => del(),
                'x-batch-delete': () => del(2),
                'x-edit': () => edit(selectRow.id, selectRow.name)
            });
        });
    </script>

</body>

</html>